// pages/gangManage/create/create.js
const app = getApp()
var http = require('../../../utils/server.js');
var util = require('../../../utils/util.js');
Page({
      data: {
        isread: false,
        step: 0,
        stepName: false,
        stepAvatar: false,
        stepPhone: false,
        gangPhone: '',
        img: {},
        codeMsg: '获取验证码',
        isSendedMsg: false,
        showAuthor: false,
      },

      onLoad: function(option) {
        var that = this
        const scene = decodeURIComponent(option.scene)
        var csid = scene || ''
        that.setData({
          csid: csid
        })
        //用户信息
        app.getUserInfo(function(userInfo) {
          if (userInfo != null) {
            console.info('用户信息', userInfo)
            that.setData({
              userInfo: {
                gangRole: userInfo.gangRole,
                gangId: userInfo.gangId,
                headUrl: userInfo.avatarUrl,
                name: userInfo.nickName
              },
              showAuthor: false
            })
            that.checkGang(userInfo)
          } else {
            that.setData({
              showAuthor: true
            })
          }
        })
      },

      onParentEvent: function(e) {
        var that = this
        app.loginServer(app.globalData.code, e.detail.encryptedData, e.detail.iv, that.checkGang)
      },
      checkGang: function(userInfo) {
        var that = this
        if (userInfo != null) {
          that.setData({
            showAuthor: false
          })
          if (userInfo.gangId != '') {
            util.showError('您已经有帮会了', function() {
              wx.setStorageSync('userInfo', null);
              console.log('您已经有帮会了')
              wx.reLaunch({
                url: '/pages/index/welcome'
              })
            })
            http.post({
              url: "gang/checkHaveGang"
            }, function(data) {
              if (data != 'ok') {
                util.showError('您已经有帮会了', function() {
                  wx.setStorageSync('userInfo', null);
                  wx.reLaunch({
                    url: '/pages/index/welcome'
                  })
                })
              }})
            }
          } else {
            that.setData({
              showAuthor: true
            })
          }
        },
        checkboxChange: function(e) {
            var that = this
            that.setData({
              isread: !that.data.isread
            })
          },
          next: function(e) {
            var that = this
            if (that.data.step == 1) {
              // 检查帮会名
              http.post({
                url: "gang/checkGangName",
                name: that.data.gangName
              }, function(data) {
                that.setData({
                  step: that.data.step + 1
                })
              })
            } else if (that.data.step == 2) {
              that.setData({
                gangAvatar: that.data.img.src,
                step: that.data.step + 1
              })
            } else if (that.data.step == 3) {
              // 检查验证码正确
              http.post({
                url: "gang/checkVerificationCode",
                phone: that.data.gangPhone,
                code: that.data.verificationCode,
              }, function(data) {
                that.setData({
                  step: that.data.step + 1
                })
              })
            } else {
              that.setData({
                step: that.data.step + 1
              })
            }
          },
          prev: function(e) {
            var that = this
            that.setData({
              step: that.data.step - 1
            })
          },
          getVerificationCode: function(e) {
            var that = this
            if (that.data.gangPhone.length == 11) {
              if (!that.data.isSendedMsg) {
                var time = 59
                var si = setInterval(function() {
                  time--
                  console.log('定时器')
                  that.setData({
                    codeMsg: '重新获取(' + time + ')'
                  })
                  if (time <= 0) {
                    clearInterval(si)
                    that.setData({
                      isSendedMsg: false,
                      codeMsg: '获取验证码'
                    })
                  }
                }, 1000)
                that.setData({
                  isSendedMsg: true
                })
                http.post({
                  url: "gang/sendVerificationCode",
                  phone: that.data.gangPhone,
                }, function(data) {
                  util.showSuccess('短信已发送')
                })
              }
            } else
              util.showModal('请输入正确的手机号')

          },
          bindName: function(e) {
            var stepName = false
            if (e.detail.value.length > 0)
              stepName = true
            this.setData({
              gangName: e.detail.value,
              stepName: stepName
            })
          },
          addPic: function(e) {
            var that = this
            wx.chooseImage({
              count: 1,
              sizeType: ['compressed'],
              success: function(res) {
                var tempFilePaths = res.tempFilePaths
                var img = that.data.img
                img = {
                  src: tempFilePaths[0],
                  cover: true,
                  coverHeight: 100
                }
                that.setData({
                  img: img
                })

                var token = wx.getStorageSync('token') || "";
                const uploadTask = wx.uploadFile({
                  url: 'https://www.ymqvip.com/api/upload/uploadPic',
                  filePath: tempFilePaths[0],
                  name: 'imgfile',
                  formData: {
                    token: token,
                    type: 'center'
                  },
                  success: function(res) {
                    var uploadData = JSON.parse(res.data)
                    if (uploadData.code == 0) {
                      console.log('上传成功', uploadData.data)
                      var img = that.data.img
                      img.src = uploadData.data
                      img.cover = false
                      that.setData({
                        img: img,
                        stepAvatar: true
                      })
                    } else {
                      util.showModal("上传出错，" + res.message)
                      return false;
                    }
                  },
                  fail: function(e) {
                    console.log('上传出错', e)
                    util.showModal("上传出错，请联系管理员。")
                    return false;
                  }
                })

                uploadTask.onProgressUpdate((res) => {
                  var img = that.data.img
                  img.coverHeight = 100 - res.progress
                  that.setData({
                    img: img
                  })
                })

              }
            })

          },
          bindCode: function(e) {
            var stepPhone = false
            if (e.detail.value.length > 0 && this.data.gangPhone.length == 11)
              stepPhone = true
            this.setData({
              verificationCode: e.detail.value,
              stepPhone: stepPhone
            })
          },
          bindPhone: function(e) {
            this.setData({
              gangPhone: e.detail.value
            })
          },
          formSubmit: function(e) {
            var formId = e.detail.formId;
            var that = this
            var pics = [];
            for (p in that.data.imgs) {
              pics.push(that.data.imgs[p].src)
            }
            http.post({
              url: "gang/create",
              form_id: formId,
              name: that.data.gangName,
              phone: that.data.gangPhone,
              avatar: that.data.gangAvatar,
              csid: that.data.csid
            }, function(data) {
              util.showSuccess('创建成功', function () {
                wx.setStorageSync('userInfo', null);
                wx.reLaunch({
                  url: '/pages/index/welcome'
                })
              })
            })
          },

      })